' 生成随机数字字符串，并返回字符串
' @param length 数字字符串的长度
Public Function numStr(ByVal length As Integer) As String
    numStr = ""
    Randomize

    For i = 1 To length
        numStr = numStr & Int(Rnd * 10)
    Next
End Function

' 生成第一位不为0的随机数字，并返回字符串
' @param length 数字字符串的长度
Public Function numStrNotStartByZero(ByVal length As Integer) As String
    Randomize
    numStrNotStartByZero = Int(Rnd * 9) + 1

    For i = 2 To length
        numStrNotStartByZero = numStrNotStartByZero & Int(Rnd * 10)
    Next
End Function

' 生成指定区间内的随机数, 生成范围 [min, max]
' @param min 最小值
' @param max 最大值
Public Function numInRange(ByVal min As Integer, ByVal max As Integer) As Integer

    Randomize

    numInRange = Int(Rnd * (max - min + 1)) + min
End Function

' 随机生成全小写字母的字符串
' @param length 字符串长度
Public Function lowerEnStr(ByVal length As Integer) As String
    lowerEnStr = ""
    Randomize

    For i = 1 To length
        lowerEnStr = lowerEnStr & Chr(Int(Rnd * 26) + 97)
    Next

End Function

' 随机生成全大写字母的字符串
' @param length 字符串长度
Public Function upperEnStr(ByVal length As Integer) As String
    upperEnStr = ""
    Randomize

    For i = 1 To length
        upperEnStr = upperEnStr & Chr(Int(Rnd * 26) + 65)
    Next

End Function

' 随机生成全字母的字符串，同时包含大写，小写字母
' @param length 字符串长度
Public Function enStr(ByVal length As Integer) As String
    enStr = ""
    Randomize

    Dim lowerOrUpper As Integer
    For i = 1 To length
        lowerOrUpper = Int(Rnd * 2) + 1
        If lowerOrUpper = 1 Then
            ' upper
            enStr = enStr & Chr(Int(Rnd * 26) + 65)
        Else
            ' lower
            enStr = enStr & Chr(Int(Rnd * 26) + 97)
        End If

    Next
End Function

' 随机生成字符串，包含大写字母 + 小写字母 + 数字0-9
' @param length 字符串长度
Public Function enNumStr(ByVal length As Integer) As String
    enNumStr = ""
    Randomize

    Dim lowerOrUpper As Integer
    Dim enOrNum As Integer
    Dim i As Integer
    For i = 1 To length
        enOrNum = Int(Rnd * 2) + 1

        If enOrNum = 1 Then
            ' 生成数字
            enNumStr = enNumStr & Int(Rnd * 10)
        Else
            ' 生成字母
            lowerOrUpper = Int(Rnd * 2) + 1
            If lowerOrUpper = 1 Then
                ' 生成大写字母
                enNumStr = enNumStr & Chr(Int(Rnd * 26) + 65)
            Else
                ' 生成小写字母
                enNumStr = enNumStr & Chr(Int(Rnd * 26) + 97)
            End If
        End If
    Next
End Function

' 生成指定年范围内的随机日期
' @param startYear 开始年限
' @param endYear 结束年限
' @return yyyy-MM-dd
Public Function yyyyMMddInYearRange(ByVal startYear As Integer, ByVal endYear As Integer) As String
    Dim yearStr As String
    Randomize

    If startYear >= endYear Then
        yearStr = startYear
    Else
        yearStr = startYear + Int(Rnd * (endYear - startYear + 1))
    End If

    Dim monthStr As String
    monthStr = UtilsString.addLeftZeroForNum(Int(Rnd * 12) + 1, 2)

    Dim dayStr As String
    If monthStr = "01" Or _
           monthStr = "03" Or _
           monthStr = "05" Or _
           monthStr = "07" Or _
           monthStr = "08" Or _
           monthStr = "10" Or _
           monthStr = "12" Then
        dayStr = UtilsString.addLeftZeroForNum(Int(Rnd * 31) + 1, 2)
    ElseIf monthStr = "02" Then
        dayStr = UtilsString.addLeftZeroForNum(Int(Rnd * 28) + 1, 2)
    Else
        dayStr = UtilsString.addLeftZeroForNum(Int(Rnd * 30) + 1, 2)
    End If

    yyyyMMddInYearRange = yearStr & monthStr & dayStr
End Function

' 生成指定年范围内的随机时间戳
' @param startYear 开始年限
' @param endYear 结束年限
' @return yyyy-MM-dd HH:mm:ss.xxx
Public Function timestamp(ByVal startYear As Integer, ByVal endYear As Integer) As String
    Dim yearStr As String
    Randomize

    If startYear >= endYear Then
        yearStr = startYear
    Else
        yearStr = startYear + Int(Rnd * (endYear - startYear + 1))
    End If

    Dim monthStr As String
    monthStr = UtilsString.addLeftZeroForNum(Int(Rnd * 12) + 1, 2)

    Dim dayStr As String
    If monthStr = "01" Or _
           monthStr = "03" Or _
           monthStr = "05" Or _
           monthStr = "07" Or _
           monthStr = "08" Or _
           monthStr = "10" Or _
           monthStr = "12" Then
        dayStr = UtilsString.addLeftZeroForNum(Int(Rnd * 31) + 1, 2)
    ElseIf monthStr = "02" Then
        dayStr = UtilsString.addLeftZeroForNum(Int(Rnd * 28) + 1, 2)
    Else
        dayStr = UtilsString.addLeftZeroForNum(Int(Rnd * 30) + 1, 2)
    End If

    timestamp = yearStr & "-" & monthStr & "-" & dayStr & _
                " " & UtilsString.addLeftZeroForNum(UtilsRandom.numInRange(0,23), 2) & _
                ":" & UtilsString.addLeftZeroForNum(UtilsRandom.numInRange(0,59), 2) & _
                ":" & UtilsString.addLeftZeroForNum(UtilsRandom.numInRange(0,59), 2) & _
                "." & UtilsRandom.numStr(3)
End Function
